﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace winform_前后端分离
{
    public partial class Form1 : Form
    {
        string file;
        List<string> LogList;

        public Form1()
        {
            InitializeComponent();
            LogList = new List<string>();

            //构建绑定关系
            /*
            1.定义了一个Observable对象（可观察对象）
            2.间隔100毫秒发送一次通知
            3.针对当前UI控件来设置调度器，这个调度器主要是确保下面的订阅代码在UI线程上执行
            4.得到通知之后，更新progressBar1
            */
            
            Observable
              .Interval(TimeSpan.FromMilliseconds(100))
              .ObserveOn(this.progressBar1)
              .Subscribe(r =>
              {
                  this.progressBar1.Step = this.LogList.Count;
                  this.progressBar1.PerformStep();
              });

            Observable.Interval(TimeSpan.FromMilliseconds(100))
          .ObserveOn(this.label1)
          .Subscribe(r =>
          {
              this.label1.Text = LogList.Count.ToString();
          });


            Observable.Interval(TimeSpan.FromMilliseconds(10))
              .ObserveOn(this.label1)
              .Subscribe(r =>
              {
                  this.textBox1.Text = file;
              });

            Observable.Interval(TimeSpan.FromMilliseconds(100))
            .ObserveOn(this.listBox1)
            .Subscribe(r =>
            {
                //定时循环会引发循环卡死的情况，这个是定时触发的动作
                foreach (string item in LogList)
                {
                    this.listBox1.Items.Add(item);
                }

            });

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //string file = this.openFileDialog1.FileName;
            string file = "Log.txt";
            this.textBox1.Text = file;
            string[] FileArray =  File.ReadAllLines(file);
            this.progressBar1.Maximum = FileArray.Length;
            foreach (string item in FileArray)
            {
                this.listBox1.Items.Add(item);
                this.progressBar1.Step = this.listBox1.Items.Count;
                this.progressBar1.PerformStep();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //主逻辑，插入列表，不考虑更新进度条
            file = "Log.txt";
            foreach (var item in File.ReadAllLines(file))
            {
                LogList.Add(item);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
